package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.FaJiaoListBean;
import com.weimei.conversionTreasure.http.entity.PaoLiangListBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.weight.BottomDelPop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class DRVM extends TitleVM {

    public ObservableField<String> type = new ObservableField<>();

    public ObservableList<PLItemVM> observableListPL = new ObservableArrayList<>();
    public ItemBinding<PLItemVM> itemBindingPL = ItemBinding.of(BR.viewModel, R.layout.item_pl);

    public ObservableList<FJItemVM> observableListFJ = new ObservableArrayList<>();
    public ItemBinding<FJItemVM> itemBindingFJ = ItemBinding.of(BR.viewModel, R.layout.item_nj);

    //右边文字的观察者
    public ObservableInt emptyView = new ObservableInt(View.GONE);
    public ObservableInt njRecy = new ObservableInt(View.GONE);
    public ObservableInt plRecy = new ObservableInt(View.GONE);

    public DRVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("详细记录");
    }

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        mActivity = activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (type.get().equals("0")){
            getFjList();
            njRecy.set(View.VISIBLE);
            plRecy.set(View.GONE);
        }else {
            getPlList();
            njRecy.set(View.GONE);
            plRecy.set(View.VISIBLE);
        }
    }

    private void getFjList() {
        observableListFJ.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.fermentList(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<FaJiaoListBean>() {
                    @Override
                    public void onNext(FaJiaoListBean listBean) {
                        if (listBean.issucc)
                            if (ValidateUtils.isNotEmptyObjectOrString(listBean.getData())){
                                if (listBean.getData().size() > 0){
                                    emptyView.set(View.GONE);
                                    for (FaJiaoListBean.DataDTO date : listBean.getData()) {
                                        FJItemVM fjItemVM = new FJItemVM(DRVM.this,date);
                                        fjItemVM.setViewModel(DRVM.this);
                                        observableListFJ.add(fjItemVM);
                                    }
                                }else {
                                    emptyView.set(View.VISIBLE);
                                }

                            }else {
                                emptyView.set(View.VISIBLE);
                            }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getPlList() {
        observableListPL.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.soakgrainList(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<PaoLiangListBean>() {
                    @Override
                    public void onNext(PaoLiangListBean listBean) {
                        if (listBean.issucc)
                            if (ValidateUtils.isNotEmptyObjectOrString(listBean.getData())){
                                if (listBean.getData().size() > 0){
                                    emptyView.set(View.GONE);
                                    for (PaoLiangListBean.DataDTO date : listBean.getData()) {
                                        PLItemVM plItemVM = new PLItemVM(DRVM.this,date);
                                        plItemVM.setViewModel(DRVM.this);
                                        observableListPL.add(plItemVM);
                                    }
                                }else {
                                    emptyView.set(View.VISIBLE);
                                }

                            }else {
                                emptyView.set(View.VISIBLE);
                            }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void delNotice(int id){
        new XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .asCustom(new BottomDelPop(mActivity, s1 -> {
                    if (type.get().equals("0")){
                        delFjList(id);
                    }else {
                        delPlList(id);
                    }
                },"删除提醒"))
                .show();
    }

    private void delPlList(int id) {
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid", id+"");
        model.delete_soakgrain(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult result) {
                        if (result.issucc){
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void delFjList(int id) {
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid", id+"");
        model.delete_ferment(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult result) {
                        if (result.issucc){
                            onResume();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
